package com.leisurexi.aop.features.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;


/**
 * Aspect 配置类
 *
 * @author: leisurexi
 * @date: 2021-05-21 15:17
 */
@Aspect
@Order
public class AspectConfig {

    @Pointcut("execution(public * *(..))")
    private void anyPublicMethod() { // 方法名即 pointcut 名
        System.out.println("@Pointcut at any public method");
    }

    @Around("anyPublicMethod()")
    public Object aroundAnyPublicMethod(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("@Around any public method.");
        return pjp.proceed();
    }

    @Before("anyPublicMethod()")
    public void beforeAnyPublicMethod() {
        System.out.println("@Before any public method.");
//        throw new RuntimeException(); // 测试 AfterReturning 执行
    }

    @After("anyPublicMethod()")
    public void afterAnyPublicMethod() {
        System.out.println("@After any public method.");
    }

    @AfterReturning("anyPublicMethod()")
    public void afterReturningAnyPublicMethod() {
        System.out.println("@AfterReturning any public method.");
    }

    @AfterThrowing("anyPublicMethod()")
    public void afterThrowingAnyPublicMethod() {
        System.out.println("@AfterThrowing any public method.");
    }

}
